package com.taxipark.domain;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class TaxiManager extends Human {
	private int id;

	public int getId() {
		Connection conn = null;
		Statement s = null;
		try {
			String userName = "root";
			String password = "root";
			String url = "jdbc:mysql://localhost:3306/db_taxidrivers";
			Class.forName("com.mysql.jdbc.Driver");
			conn = DriverManager.getConnection(url, userName, password);
			System.out.println("Database connection established");

				conn.setAutoCommit(false);
			s = conn.createStatement();
			String query = "SELECT id FROM managers WHERE name='" + this.getName()+"' AND contact='"+this.getContact()+"';";
			ResultSet rs = s.executeQuery(query);

			while (rs.next()) {
				int id = rs.getInt("id");
				this.id = id;
			}
			conn.commit();
			conn.setAutoCommit(true);
		} catch (Exception e) {
			System.err.println("Cannot connect to database server");
			try {
				conn.rollback();
				conn.setAutoCommit(true);
				} catch (SQLException e2) {}
		} finally {
			if (conn != null) {
				try {
					conn.close();
					System.out.println("Database connection terminated");
				} catch (Exception e) { /* ignore close errors */
				}
			}
		}
		 
		
			
			return this.id;
					
		}
		
		

	public void setId(int id) {
		this.id = id;
	}



	public TaxiManager(String name, String contact) {
		super(name, contact);
	}

	@Override
	public String toString() {
		return "TaxiManager [name=" + getName() + ", contact=" + getContact() + "]";
	}

	public void activate(TaxiDriver driver) {
		driver.setActivity(true);
		System.out.print("\nDriver " + driver.getName() + " activated");
	}

	public void deactivate(TaxiDriver driver) {
		driver.setActivity(false);
		System.out.print("\nDriver " + driver.getName() + " deactivated");
	}
}
